package com.lanchetech.user.controller;

import com.lanchetech.bean.dto.ActivityDTO;
import com.lanchetech.bean.dto.OrderItemDTO;
import com.lanchetech.bean.request.*;
import com.lanchetech.bean.response.*;
import com.lanchetech.bean.vo.*;
import com.lanchetech.entity.*;
import com.lanchetech.service.LogisticsService;
import com.lanchetech.user.config.ReturnPayInfoVO;
import com.lanchetech.user.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(value = "订单接口", tags = "订单接口")
@RequestMapping("/api/order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    LogisticsService logisticsService;


    @ApiOperation("获取确认订单商品列表信息")
    @PostMapping("/getConfirmOrderSkuInfo")
    public ResultData<List<OrderSkuItemVO>> getConfirmOrderSkuInfo(@RequestBody List<OrderItemDTO> req,
                                                                   @AuthenticationPrincipal User user) {
        return orderService.getConfirmOrderSkuInfo(req, user.getId());
    }

    @ApiOperation("获取确认订单汇总信息（商品列表、用户收货地址、可使用积分、可使用优惠券）")
    @PostMapping("/getConfirmOrderDetail")
    public ResultData<ConfirmOrderResp> getConfirmOrderDetail(@AuthenticationPrincipal User user, @RequestBody List<OrderItemDTO> req) {
        return orderService.getConfirmOrderDetail(user, req);
    }

    @ApiOperation("创建商品订单【微信支付】")
    @PostMapping("/createWechatPayOrder")
    public ResultData<ReturnPayInfoVO> createWechatPayOrder(@AuthenticationPrincipal User user,
                                                            @RequestBody OrderReq req,
                                                            HttpServletRequest httpServletRequest) {
        return orderService.createWechatPayOrder(user, req, httpServletRequest);
    }

    @PostMapping("计算价格")
    public ResultData<ActivityDTO> calculatePrice() {
        return null;
    }

    @ApiOperation(value = "购买商品微信支付成功后，处理订单状态【微信支付回调】")
    @RequestMapping(value = "/wechatOrderNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String wechatOrderNotify(@RequestBody String xmlData) {
        return orderService.wechatOrderNotify(xmlData);
    }

    @ApiOperation("查询订单状态")
    @GetMapping("/getOrderStatus")
    public ResultData<Byte> getOrderStatus(@RequestParam("id") Long id) {
        return orderService.getOrderStatus(id);
    }

    @ApiOperation("添加到购物车")
    @PostMapping("/addToShoppingCart")
    public BaseResp addToShoppingCart(@AuthenticationPrincipal User user, @RequestBody ShoppingCart shoppingCart) {
        return orderService.addToShoppingCart(user, shoppingCart);
    }

    @ApiOperation("获取购物车商品列表信息")
    @PostMapping("/getShoppingCartSkuInfo")
    public ResultData<ShoppingCartVO> getShoppingCartSkuInfo(@AuthenticationPrincipal User user) {
        return orderService.getShoppingCartSkuInfo(user);
    }

    @ApiOperation("从购物车中删除商品")
    @GetMapping("/deleteShoppingCart")
    public BaseResp deleteShoppingCart(@AuthenticationPrincipal User user, @RequestParam(value = "skuId", required = false) Long skuId, @RequestParam(value = "skuIds", required = false) String skuIds) {
        return orderService.deleteShoppingCart(user, skuId, skuIds);
    }

    @ApiOperation("获取订单分页")
    @PostMapping("/getOrderPage")
    public ResultData<BasePageResp<OrderResp>> getOrderPage(@AuthenticationPrincipal User user, @RequestBody PageReq req) {
        return orderService.getOrderPage(user, req);
    }

    @ApiOperation("获取商家订单分页")
    @PostMapping("/getShopOrderPage")
    public ResultData<BasePageResp<OrderResp>> getShopOrderPage(@AuthenticationPrincipal User user, @RequestBody PageReq req) {
        return orderService.getShopOrderPage(user, req);
    }

    @ApiOperation("根据收货人姓名或手机号获取userId")
    @GetMapping("/getUserIdByNameOrPhone")
    public ResultData<Long> getUserIdByNameOrPhone(@AuthenticationPrincipal User user, @RequestParam("param") String param) {
        return orderService.getUserIdByNameOrPhone(user, param);
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/getOrderDetail")
    public ResultData<OrderDetailResp> getOrderDetail(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId) {
        return orderService.getOrderDetail(user, orderId);
    }

    @ApiOperation("获取订单详情(商家查看)")
    @GetMapping("/getOrderDetailForShop")
    public ResultData<OrderDetailResp> getOrderDetailForShop(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId) {
        return orderService.getOrderDetailForShop(user, orderId);
    }

    @ApiOperation("删除订单(仅隐藏)")
    @GetMapping("/deleteOrders")
    public BaseResp deleteOrders(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return orderService.deleteOrders(user, id);
    }

    @ApiOperation("获取查看订单评价")
    @GetMapping("/getOrderCommentDetail")
    public ResultData<OrderComment> getOrderCommentDetail(@AuthenticationPrincipal User user, @RequestParam("orderSkuId") Long orderSkuId) {
        return orderService.getOrderCommentDetail(user, orderSkuId);
    }

    @ApiOperation("添加或编辑评价")
    @PostMapping("/editOrderComment")
    public BaseResp editOrderComment(@AuthenticationPrincipal User user, @RequestBody OrderComment orderComment) {
        return orderService.editOrderComment(user, orderComment);
    }

    @ApiOperation("关闭和开启评价")
    @GetMapping("/toggleOrderComment")
    public BaseResp toggleOrderComment(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return orderService.toggleOrderComment(user, id);
    }

    @ApiOperation("删除订单评价")
    @GetMapping("/deleteComment")
    public BaseResp deleteComment(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return orderService.deleteComment(user, id);
    }

    @ApiOperation("添加或编辑售后")
    @PostMapping("/editAfterSale")
    public BaseResp editAfterSale(@AuthenticationPrincipal User user, @RequestBody AfterSale afterSale) {
        return orderService.editAfterSale(user, afterSale);
    }

    @ApiOperation("添加或编辑售后退货物流信息")
    @PostMapping("/editAfterSaleLogistics")
    public BaseResp editAfterSaleLogistics(@AuthenticationPrincipal User user, @RequestBody AfterSale afterSale) {
        return orderService.editAfterSaleLogistics(user, afterSale);
    }

    @ApiOperation("获取售后详情")
    @GetMapping("/getAfterSale")
    public ResultData<AfterSale> getAfterSale(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return orderService.getAfterSale(user, id);
    }

    @ApiOperation("取消售后")
    @GetMapping("/cancelAfterSale")
    public BaseResp cancelAfterSale(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return orderService.cancelAfterSale(user, id);
    }

    @ApiOperation("获取售后分页")
    @PostMapping("/getAfterSalePage")
    public ResultData<BasePageResp<AfterSaleResp>> getAfterSalePage(@AuthenticationPrincipal User user, @RequestBody PageReq req) {
        return orderService.getAfterSalePage(user, req);
    }

    @ApiOperation("获取订单物流信息")
    @GetMapping("/getLogisticsTrace")
    public ResultData<String> getLogisticsTrace(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId) {
        return orderService.getLogisticsTrace(user, orderId);
    }

    @ApiOperation("获取订单物流信息和收货地址")
    @GetMapping("/getLogisticsTraceAndAddress")
    public ResultData<LogisticsAndAddressVO> getLogisticsTraceAndAddress(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId) {
        return orderService.getLogisticsTraceAndAddress(user, orderId);
    }

    @ApiOperation("获取订单明细详情和地址")
    @GetMapping("/getOrderSkuAndOrderAddress")
    public ResultData<OrderSkuAndOrderAddressVO> getOrderSkuAndOrderAddress(@AuthenticationPrincipal User user, @RequestParam("orderSkuId") Long orderSkuId) {
        return orderService.getOrderSkuAndOrderAddress(user, orderSkuId);
    }

    @ApiOperation("确认收货")
    @GetMapping("/confirmOrder")
    public BaseResp confirmOrder(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId) {
        return orderService.confirmOrder(user, orderId);
    }

    @ApiOperation("获取商品提货二维码信息")
    @PostMapping("/getOrderQrcodeInfo")
    public ResultData<OrderQrcodeInfoVO> getOrderQrcodeInfo(@RequestBody PageReq req) {
        return orderService.getOrderQrcodeInfo(req);
    }

    @ApiOperation("商店核销时获取订单信息")
    @GetMapping("/verifyOrderInfoByHashId")
    @PreAuthorize("hasRole('ROLE_CLERK')")
    public ResultData<OrderSkuVO> verifyOrderInfoByHashId(@RequestParam("hashId") String hashId) {
        return orderService.verifyOrderInfoByHashId(hashId);
    }

    @ApiOperation("商店确认核销订单商品")
    @GetMapping("/verifyConfirmOrderSku")
    @PreAuthorize("hasRole('ROLE_CLERK')")
    public BaseResp verifyConfirmOrderSku(@RequestParam("hashId") String hashId) {
        return orderService.verifyConfirmOrderSku(hashId);
    }

    @ApiOperation("获取好友订单分页")
    @PostMapping("/getFriendOrderPage")
    public ResultData<BasePageResp<UserCommissionResp>> getFriendOrderPage(@AuthenticationPrincipal User user, @RequestBody UserCommissionReq req) {
        return orderService.getFriendOrderPage(user, req);
    }

    @ApiOperation("获取商家订单推荐")
    @PostMapping("/getShopOrderRecommendPage")
    public ResultData<BasePageResp<OrderSkuRecommendVO>> getShopOrderRecommendPage(@RequestBody PageReq req) {
        return orderService.getShopOrderRecommendPage(req);
    }

    @ApiOperation("获取用户订单推荐购买列表")
    @PostMapping("/getUserRecommendPage")
    public ResultData<BasePageResp<OrderSkuRecommendVO>> getUserRecommendPage(@RequestBody PageReq req) {
        return orderService.getUserRecommendPage(req);
    }

    @ApiOperation("填写预约客户姓名和手机号")
    @PostMapping("/updateOrderNameAndPhone")
    public BaseResp updateOrderNameAndPhone(@RequestBody Orders req) {
        return orderService.updateOrderNameAndPhone(req);
    }

    @ApiOperation("获取代付订单信息，仅在24小时内有效")
    @GetMapping("/anotherPayOrderDetail")
    public ResultData<AnotherPayOrderVO> anotherPayOrderDetail(@RequestParam("orderId") Long orderId) {
        return orderService.anotherPayOrderDetail(orderId);
    }

    @ApiOperation("为他人代付")
    @GetMapping("/payForOtherOrder")
    public ResultData<ReturnPayInfoVO> payForOtherOrder(@AuthenticationPrincipal User user, @RequestParam("orderId") Long orderId, HttpServletRequest httpServletRequest) {
        return orderService.payForOtherOrder(user, orderId, httpServletRequest);
    }

    @ApiOperation("我的代付记录分页")
    @PostMapping("/getAnotherPayOrderPage")
    public ResultData<BasePageResp<OrderPayVO>> getAnotherPayOrderPage(@RequestBody OrderPayReq req) {
        return orderService.getAnotherPayOrderPage(req);
    }

    @ApiOperation("获取抢购活动详情页信息")
    @GetMapping("/getRushOrderDetail")
    public ResultData<RushOrderDetailVO> getRushOrderDetail(@RequestParam("orderId") Long orderId) {
        return orderService.getRushOrderDetail(orderId);
    }

    @ApiOperation("抢购订单分页")
    @PostMapping("/getRushOrderPage")
    public ResultData<BasePageResp<RushOrderResp>> getRushOrderPage(@RequestBody RushOrderReq req) {
        return orderService.getRushOrderPage(req);
    }

    @ApiOperation("抢购结果名单分页")
    @PostMapping("/getRushOrderResultPage")
    public ResultData<BasePageResp<RushOrderSkuVO>> getRushOrderResultPage(@RequestBody RushOrderReq req) {
        return orderService.getRushOrderResultPage(req);
    }
}
